<template>
  <basic-container>
    <avue-crud :option="option"
               :data="data"
               ref="crud"
               v-model="form"
               :page="page"
               class="resource-list"
               :before-open="beforeOpen"
               @row-del="rowDel"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot-scope="{row}" slot="icon">
        <div style="text-align:center">
          <i :class="row.icon"></i>
        </div>
      </template>
      <template slot-scope="{row}" slot="parentId">
        <div v-if="row.parentId">
        {{row.parentName}}
        </div>
        <div v-else>
          -
        </div>
      </template>
    </avue-crud>
  </basic-container>
</template>
<script>
  import { getList, add, parent, update, remove } from "@/api/system/resource";
  import iconList from "@/config/iconList";

  export default {
    name: "resource-info",
    inheritAttrs: false,
    data() {
      return {
        form: {},
        query: {},
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        option: {
          tip: false,
          border: true,
          index: true,
          indexLabel: '序号',
          size: 'small',
          columnBtn: false,
          refreshBtn: false,
          searchMenuSpan: 8,
          menuWidth: 160,
          defaultExpandAll: true,
          column: [
            {
              label: "资源编码",
              prop: "code",
              search: true,
              rules: [
                { required: true, message: "请输入资源编码", trigger: "blur" }
              ]
            },
            {
              label: "资源名称",
              prop: "name",
              search: true,
              rules: [
                { required: true, message: "请输入资源名称", trigger: "blur" }
              ]
            },
            {
              label: "路由地址",
              prop: "path",
              rules: [
                { required: true, message: "请输入路由地址", trigger: "blur" }
              ]
            },
            {
              label: "上级资源",
              prop: "parentId",
              type: "tree",
              dicData: [],
              slot: true,
              props: {
                value: 'id',
                label: "title"
              }
            },
            {
              label: "资源图标",
              prop: "icon",
              type: "icon-select",
              slot: true,
              iconList: iconList,
              rules: [
                { required: true, message: "请选择资源图标", trigger: "click" }
              ]
            },
            {
              label: "资源类型",
              prop: "category",
              type: "radio",
              dicData: [
                {
                  label: "菜单",
                  value: "1"
                },
                {
                  label: "按钮",
                  value: "2"
                }
              ],
              hide: true,
              rules: [
                { required: true, message: "请选择资源类型", trigger: "blur" }
              ]
            },
            {
              label: "资源别名",
              prop: "alias",
              hide: true,
              rules: [
                { required: true, message: "请输入资源别名", trigger: "blur" }
              ]
            },
            {
              label: "资源排序",
              prop: "sortOrder",
              type: 'number',
              span: 8,
              search: false,
              hide: true,
              value: 1,
              minRows: 1,
              maxRows: 999
            },
            {
              label: "资源说明",
              prop: "remark",
              type: "textarea",
              span: 24,
              minRows: 3,
              hide: true
            }
          ]
        },
        data: []
      }
    },
    methods: {
      onLoad(page, params = {}) {
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data;
          this.page.total = data.total;
          this.data = data.records;
        });
      },
      beforeOpen(done, type) {
        // 获取父级资源
        const index = this.$refs.crud.findColumnIndex("parentId");
        const cindex = this.$refs.crud.findColumnIndex("category");
        this.option.column[cindex].disabled = false;
        if (["edit", "view"].includes(type)) {
          parent(this.form.id).then(res => {
            this.option.column[index].dicData = res.data;
            this.option.column[index].disabled = true;
            if (res.data) {
              this.setTreeDisabled(res.data, cindex);
            }
          });
        } else {
          parent(this.form.id).then(res => {
            this.option.column[index].dicData = res.data;
            this.option.column[index].disabled = false;
          });
        }
        done();
      },
      rowSave(row, loading, done) {
        add(row).then(ret => {
          if (ret.success) {
            loading();
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          } else {
            done();
            this.$message({
              type: "error",
              message: ret.msg
            });
          }
        }, error => {
          done();
          console.log(error);
        })
      },
      rowUpdate(row, index, loading, done) {
        update(row).then(ret => {
          if (ret.success) {
            loading();
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          } else {
            done();
            this.$message({
              type: "error",
              message: ret.msg
            });
          }
        }, error => {
          done();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定要删除该资源吗?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row);
          })
          .then(ret => {
            if (ret.success) {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!"
              });
            } else {
              this.$message({
                type: "error",
                message: ret.msg
              });
            }
          }).catch(() => {
        });
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.onLoad(this.page, params);
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      setTreeDisabled(data, cindex) {
        let that = this
        for (let i of data) {
          if (i.id == this.form.id && i.children) {
            this.option.column[cindex].disabled = true;
            break;
          }
          if (i.children) {
            that.setTreeDisabled(i.children, cindex)
          }
        }
      }
    }
  }
</script>
<style scoped lang="scss">
  @import "~_s/placeholders";
  @import "~_s/table-list";

  ::v-deep .resource-list {
    @extend %fullsize;
    @extend %table-list;
  }
</style>